package com.xiaoxing.salesclient.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.xiaoxing.gifloadingview.LoadingDialogUtil;
import com.xiaoxing.salesclient.di.component.DaggerGeRenZhongXinYuanGongComponent;
import com.xiaoxing.salesclient.di.module.GeRenZhongXinYuanGongModule;
import com.xiaoxing.salesclient.mvp.contract.GeRenZhongXinYuanGongContract;
import com.xiaoxing.salesclient.mvp.model.entity.OrderPay;
import com.xiaoxing.salesclient.mvp.model.entity.UserCenter;
import com.xiaoxing.salesclient.mvp.model.entity.UserStaffCenter;
import com.xiaoxing.salesclient.mvp.presenter.GeRenZhongXinYuanGongPresenter;
import com.xiaoxing.salesclient.mvp.ui.adapter.GeRenZhongXinDingDanJiLuUserStaffCenterAdapter;
import com.xiaoxing.salesclient.mvp.ui.adapter.GeRenZhongXinHuiZhenShiJianAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import me.jessyan.armscomponent.commonres.utils.ToolbarUtils;
import me.jessyan.armscomponent.commonsdk.core.BaseConstants;
import me.jessyan.armscomponent.commonsdk.core.RouterHub;
import xiaoxing.com.salesclient.R;
import xiaoxing.com.salesclient.R2;

import static com.jess.arms.utils.Preconditions.checkNotNull;


@Route(path = RouterHub.SALES_CLIENT_GE_REN_ZHONG_XIN_YUAN_GONG_ACTIVITY)
public class GeRenZhongXinYuanGongActivity extends BaseActivity
        <GeRenZhongXinYuanGongPresenter> implements GeRenZhongXinYuanGongContract.View, GeRenZhongXinDingDanJiLuUserStaffCenterAdapter.Pay

{
    @BindView(R2.id.cimg_head)
    ImageView cimgHead;
    @BindView(R2.id.tv_xing_ming)
    TextView tvXingMing;
    @BindView(R2.id.tv_zhang_hao)
    TextView tvZhangHao;
    @BindView(R2.id.tv_sheng_yu_jin_e)
    TextView tvShengYuJinE;
    @BindView(R2.id.tv_lei_ji_ji_fen)
    TextView tvLeiJiJiFen;
    @BindView(R2.id.tv_shang_pin_dui_huan)
    TextView tvShangPinDuiHuan;
    @BindView(R2.id.img_level)
    ImageView imgLevel;
    @BindView(R2.id.img_vip)
    ImageView imgVip;
    @BindView(R2.id.img_er_wei_ma)
    ImageView imgErWeiMa;
    @BindView(R2.id.ll_no_data_ding_dan_ji_lu)
    LinearLayout tvDingDanTip;
    @BindView(R2.id.ll_no_data_hui_zhen_shi_jian)
    LinearLayout tvHuiZhenTip;
    @BindView(R2.id.tv_dian_hua)
    TextView tvDianHua;
    @BindView(R2.id.tv_di_zhi)
    TextView tvDiZhi;
    @BindView(R2.id.tv_email)
    TextView tvEmail;

    private GeRenZhongXinDingDanJiLuUserStaffCenterAdapter mGeRenZhongXinDingDanJiLuAdapter;
    private GeRenZhongXinHuiZhenShiJianAdapter mGeRenZhongXinHuiZhenShiJianAdapter;
    private RecyclerView mGeRenZhongXinDingDanJiLuRecyclerView;
    private RecyclerView mGeRenZhongXinHuiZhenShiJianRecyclerView;
    private List<UserStaffCenter.DataBean.OrderBean> mOrderBeanList = new ArrayList<>();
    private List<UserCenter.DataBean.HistoryBean> mHistoryBeanList = new ArrayList<>();
    private String mQRImgPath;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerGeRenZhongXinYuanGongComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .geRenZhongXinYuanGongModule(new GeRenZhongXinYuanGongModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_ge_ren_zhong_xin_yuan_gong; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

        ToolbarUtils.initToolbarTitleBack(this, "員工個人中心");

        initGeRenZhongXinDingDanJiLuRecyclerView();
        initGeRenZhongXinHuiZhenShiJianRecyclerView();

        mPresenter.userStaffCenter(mSharedPreferencesHelper.getString(BaseConstants.TOKEN));

    }


    @Override
    public void showLoading() {
        LoadingDialogUtil.showGifdialog2(getSupportFragmentManager(), R.drawable.public_loading);
    }

    @Override
    public void hideLoading() {
        LoadingDialogUtil.dismissDialog();
    }


    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    private void initGeRenZhongXinDingDanJiLuRecyclerView() {
        mGeRenZhongXinDingDanJiLuRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_ding_dan);
        mGeRenZhongXinDingDanJiLuAdapter = new GeRenZhongXinDingDanJiLuUserStaffCenterAdapter(this, mOrderBeanList, this);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mGeRenZhongXinDingDanJiLuRecyclerView.setLayoutManager(linearLayoutManager);
        mGeRenZhongXinDingDanJiLuRecyclerView.setAdapter(mGeRenZhongXinDingDanJiLuAdapter);
    }

    private void initGeRenZhongXinHuiZhenShiJianRecyclerView() {
        mGeRenZhongXinHuiZhenShiJianRecyclerView = (RecyclerView) findViewById(R.id.recyclerView_hui_zhen);
        mGeRenZhongXinHuiZhenShiJianAdapter = new GeRenZhongXinHuiZhenShiJianAdapter(this, mHistoryBeanList);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mGeRenZhongXinHuiZhenShiJianRecyclerView.setLayoutManager(linearLayoutManager);
        mGeRenZhongXinHuiZhenShiJianRecyclerView.setAdapter(mGeRenZhongXinHuiZhenShiJianAdapter);
    }

    @Override
    public void payOrder(String order_no) {
        ARouter.getInstance().build(RouterHub.SALES_CLIENT_PAY_ACTIVITY).withString("order_no", order_no).withString("is_staff", "1").navigation();
    }

    @Override
    public void userStaffCenterSuccessed(UserStaffCenter userCenter) {
        if (userCenter != null && userCenter.getData() != null) {

            UserStaffCenter.DataBean dataBean = userCenter.getData();

            tvXingMing.setText("姓名：" + dataBean.getStaff_name());
            tvZhangHao.setText("帳號：" + dataBean.getUser_name());
//            tvShengYuJinE.setText("剩餘金額：" + dataBean.getMember_money());
            tvShengYuJinE.setText("密碼：" + mSharedPreferencesHelper.getString(BaseConstants.PASSWORD));
            tvDianHua.setText("電話：" + dataBean.getMobile());
            tvDiZhi.setText("地址：" + dataBean.getAddress());
            tvEmail.setText("Email：" + dataBean.getEmail());

            RequestOptions mRequestOptions = RequestOptions.circleCropTransform()
                    .diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                    .skipMemoryCache(true);//不做内存缓存
            Glide.with(this).load(dataBean.getCover()).apply(mRequestOptions).into(cimgHead);


            if (dataBean.getOrder() != null && dataBean.getOrder().size() > 0) {
                tvDingDanTip.setVisibility(View.GONE);
                mGeRenZhongXinDingDanJiLuRecyclerView.setVisibility(View.VISIBLE);
                mOrderBeanList.clear();
                mOrderBeanList.addAll(dataBean.getOrder());
                mGeRenZhongXinDingDanJiLuAdapter.notifyDataSetChanged();
            } else {
                tvDingDanTip.setVisibility(View.VISIBLE);
                mGeRenZhongXinDingDanJiLuRecyclerView.setVisibility(View.GONE);
            }


//            if (dataBean.getHistory() != null && dataBean.getHistory().size() > 0) {
//
//                tvHuiZhenTip.setVisibility(View.GONE);
//                mGeRenZhongXinHuiZhenShiJianRecyclerView.setVisibility(View.VISIBLE);
//                mHistoryBeanList.clear();
//                mHistoryBeanList.addAll(dataBean.getHistory());
//                mGeRenZhongXinHuiZhenShiJianAdapter.notifyDataSetChanged();
//            } else {
//                tvHuiZhenTip.setVisibility(View.VISIBLE);
//                mGeRenZhongXinHuiZhenShiJianRecyclerView.setVisibility(View.GONE);
//            }
        }
    }

    @Override
    public void orderPaySuccess(OrderPay orderPay) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
